<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
   
   <meta name="description" content="string operations">
    
    <meta name="author" content="John S. Urban" >
    <link rel="icon" href="../favicon.png">

    <title>substitute &ndash; M_strings</title>

    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <link href="../css/pygments.css" rel="stylesheet">
    <link href="../css/font-awesome.min.css" rel="stylesheet">
    <link href="../css/local.css" rel="stylesheet">
    
    <link  href="../tipuesearch/tipuesearch.css" rel="stylesheet">
    
    

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    
    <script src="../js/jquery-2.1.3.min.js"></script>
    <script src="../js/svg-pan-zoom.min.js"></script>

  </head>

  <body>

    <!-- Fixed navbar -->
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="../index.html">M_strings </a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
        
            <li class="dropdown hidden-xs visible-sm visible-md hidden-lg">
              <a href="#" class="dropdown-toggle"
              data-toggle="dropdown" role="button"
              aria-haspopup="true"
     aria-expanded="false">Contents <span class="caret"></span></a>
        <ul class="dropdown-menu">
          
              
            <li><a href="../lists/files.html">Source Files</a></li>
        
        
        
            <li><a href="../lists/modules.html">Modules</a></li>
        
            
                                
            <li><a href="../lists/procedures.html">Procedures</a></li>
        
               
            <li><a href="../lists/types.html">Derived Types</a></li>
        
        
            </ul>
            </li>


<li class="visible-xs hidden-sm visible-lg"><a href="../lists/files.html">Source Files</a></li>



<li class="visible-xs hidden-sm visible-lg"><a href="../lists/modules.html">Modules</a></li>



<li class="visible-xs hidden-sm visible-lg"><a href="../lists/procedures.html">Procedures</a></li>

                             
<li class="visible-xs hidden-sm visible-lg"><a href="../lists/types.html">Derived Types</a></li>


          </ul>
        
        <form action="../search.html" class="navbar-form navbar-right" role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search" name="q" id="tipue_search_input" autocomplete="off" required>
        </div>
<!--
        <button type="submit" class="btn btn-default">Submit</button>
-->
        </form>
        
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div class="container">
    
  
  <div class="row">
    <h1>substitute
      <small>Subroutine</small>
    
    </h1>
    
<div class="row">
  <div class="col-lg-12">
<div class="well well-sm">
  <ul class="list-inline" style="margin-bottom:0px;display:inline">
     
     
     
     
    
    
     <li><i class="fa fa-list-ol"></i>
       <a data-toggle="tooltip"
    data-placement="bottom" data-html="true"
    title=" 4.4% of total for procedures.">109 statements</a>
     </li> 
     
     
     
    <li><i class="fa fa-code"></i><a href="../src/M_strings.f90"> Source File</a></li>
     
     
  </ul>
  <ol class="breadcrumb in-well text-right">
  
    
     <li><a href='../sourcefile/m_strings.f90.html'>M_strings.f90</a></li>
    
     <li><a href='../module/m_strings.html'>M_strings</a></li>
    
  
     <li class="active">substitute</li>
  </ol>
</div>
</div>
</div>
<script>
  $(function () {
  $('[data-toggle="tooltip"]').tooltip()
  })
</script>

  </div>
  
  <div class="row">
    <div class="col-md-3 hidden-xs hidden-sm visible-md visible-lg">
    
<div id="sidebar">
  
<h3>Contents</h3>
 



<div class="panel panel-primary">
  <div class="panel-heading text-left"><h3 class="panel-title"><a data-toggle="collapse" href="#vars-0">Variables</a></h3></div>
  <div id="vars-0" class="panel-collapse collapse">
    <div class="list-group">
      
      <a class="list-group-item" href="../proc/substitute.html#variable-dum1">dum1</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ic~4">ic</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ichr~3">ichr</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-id~2">id</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ier1">ier1</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-il">il</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ind~2">ind</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ir">ir</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ladd~2">ladd</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-len_new~2">len_new</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-len_old~2">len_old</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-maxlengthout">maxlengthout</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ml">ml</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-mr">mr</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-original_input_length~2">original_input_length</a>
      
    </div>
  </div>
</div>




















<div class="panel panel-primary">
  <div class="panel-heading text-left"><h3 class="panel-title">Source Code</h3></div>
  <div class="list-group">
    <a class="list-group-item" href="../proc/substitute.html#src">substitute</a>
  </div>
</div>



</div>

    </div>
    
    <div class="col-md-9" id='text'>
    <h2>
public subroutine substitute(targetline, old, new, ierr, start, end)
    
    
   
</h2>
    
  


    
    <h2 id="name">NAME</h2>
<div class="codehilite"><pre><span></span><code><span class="nv">substitute</span><span class="ss">(</span><span class="mi">3</span><span class="nv">f</span><span class="ss">)</span> <span class="o">-</span> [<span class="nv">M_strings</span>:<span class="nv">EDITING</span>] <span class="nv">subroutine</span> <span class="nv">globally</span> <span class="nv">substitutes</span>
<span class="nv">one</span> <span class="nv">substring</span> <span class="k">for</span> <span class="nv">another</span> <span class="nv">in</span> <span class="nv">string</span>
<span class="ss">(</span><span class="nv">LICENSE</span>:<span class="nv">PD</span><span class="ss">)</span>
</code></pre></div>

<h2 id="synopsis">SYNOPSIS</h2>
<div class="codehilite"><pre><span></span><code><span class="nv">subroutine</span> <span class="nv">substitute</span><span class="ss">(</span><span class="nv">targetline</span>,<span class="nv">old</span>,<span class="nv">new</span>,<span class="nv">ierr</span>,<span class="nv">start</span>,<span class="k">end</span><span class="ss">)</span>

 <span class="nv">character</span><span class="ss">(</span><span class="nv">len</span><span class="o">=*</span><span class="ss">)</span>              :: <span class="nv">targetline</span>
 <span class="nv">character</span><span class="ss">(</span><span class="nv">len</span><span class="o">=*</span><span class="ss">)</span>,<span class="nv">intent</span><span class="ss">(</span><span class="nv">in</span><span class="ss">)</span>   :: <span class="nv">old</span>
 <span class="nv">character</span><span class="ss">(</span><span class="nv">len</span><span class="o">=*</span><span class="ss">)</span>,<span class="nv">intent</span><span class="ss">(</span><span class="nv">in</span><span class="ss">)</span>   :: <span class="nv">new</span>
 <span class="nv">integer</span>,<span class="nv">intent</span><span class="ss">(</span><span class="nv">out</span><span class="ss">)</span>,<span class="nv">optional</span>  :: <span class="nv">ierr</span>
 <span class="nv">integer</span>,<span class="nv">intent</span><span class="ss">(</span><span class="nv">in</span><span class="ss">)</span>,<span class="nv">optional</span>   :: <span class="nv">start</span>
 <span class="nv">integer</span>,<span class="nv">intent</span><span class="ss">(</span><span class="nv">in</span><span class="ss">)</span>,<span class="nv">optional</span>   :: <span class="k">end</span>
</code></pre></div>

<h2 id="description">DESCRIPTION</h2>
<div class="codehilite"><pre><span></span><code><span class="nv">Globally</span> <span class="nv">substitute</span> <span class="nv">one</span> <span class="nv">substring</span> <span class="k">for</span> <span class="nv">another</span> <span class="nv">in</span> <span class="nv">string</span>.
</code></pre></div>

<h2 id="options">OPTIONS</h2>
<div class="codehilite"><pre><span></span><code> <span class="n">TARGETLINE</span>  <span class="n">input</span> <span class="n">line</span> <span class="n">to</span> <span class="n">be</span> <span class="n">changed</span><span class="p">.</span> <span class="n">Must</span> <span class="n">be</span> <span class="n">long</span> <span class="n">enough</span> <span class="n">to</span>
             <span class="n">hold</span> <span class="n">altered</span> <span class="n">output</span><span class="p">.</span>
 <span class="n">OLD</span>         <span class="n">substring</span> <span class="n">to</span> <span class="n">find</span> <span class="n">and</span> <span class="n">replace</span>
 <span class="kr">NEW</span>         <span class="n">replacement</span> <span class="n">for</span> <span class="n">OLD</span> <span class="n">substring</span>
 <span class="n">IERR</span>        <span class="n">error</span> <span class="n">code</span><span class="p">.</span> <span class="n">If</span> <span class="n">IER</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="n">bad</span> <span class="n">directive</span><span class="p">,</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="n">then</span>
             <span class="n">count</span> <span class="n">of</span> <span class="n">changes</span> <span class="n">made</span><span class="p">.</span>
 <span class="n">START</span>       <span class="n">sets</span> <span class="n">the</span> <span class="n">left</span> <span class="n">margin</span> <span class="n">to</span> <span class="n">be</span> <span class="n">scanned</span> <span class="n">for</span> <span class="n">OLD</span> <span class="n">in</span>
             <span class="n">TARGETLINE</span><span class="p">.</span>
 <span class="kr">END</span>         <span class="n">sets</span> <span class="n">the</span> <span class="n">right</span> <span class="n">margin</span> <span class="n">to</span> <span class="n">be</span> <span class="n">scanned</span> <span class="n">for</span> <span class="n">OLD</span> <span class="n">in</span>
             <span class="n">TARGETLINE</span><span class="p">.</span>
</code></pre></div>

<h2 id="examples">EXAMPLES</h2>
<p>Sample Program:</p>
<div class="codehilite"><pre><span></span><code><span class="nv">program</span> <span class="nv">demo_substitute</span>
<span class="nv">use</span> <span class="nv">M_strings</span>, <span class="nv">only</span> : <span class="nv">substitute</span>
<span class="nv">implicit</span> <span class="nv">none</span>
<span class="o">!</span> <span class="nv">must</span> <span class="nv">be</span> <span class="nv">long</span> <span class="nv">enough</span> <span class="nv">to</span> <span class="nv">hold</span> <span class="nv">changed</span> <span class="nv">line</span>
<span class="nv">character</span><span class="ss">(</span><span class="nv">len</span><span class="o">=</span><span class="mi">80</span><span class="ss">)</span> :: <span class="nv">targetline</span>

<span class="nv">targetline</span><span class="o">=</span><span class="s1">&#39;</span><span class="s">this is the input string</span><span class="s1">&#39;</span>
<span class="nv">write</span><span class="ss">(</span><span class="o">*</span>,<span class="o">*</span><span class="ss">)</span><span class="s1">&#39;</span><span class="s">ORIGINAL    : </span><span class="s1">&#39;</span><span class="o">//</span><span class="nv">trim</span><span class="ss">(</span><span class="nv">targetline</span><span class="ss">)</span>

<span class="o">!</span> <span class="nv">changes</span> <span class="nv">the</span> <span class="nv">input</span> <span class="nv">to</span> <span class="s1">&#39;</span><span class="s">THis is THe input string</span><span class="s1">&#39;</span>
<span class="k">call</span> <span class="nl">substitute</span><span class="ss">(</span><span class="nv">targetline</span>,<span class="s1">&#39;</span><span class="s">th</span><span class="s1">&#39;</span>,<span class="s1">&#39;</span><span class="s">TH</span><span class="s1">&#39;</span><span class="ss">)</span>
<span class="nv">write</span><span class="ss">(</span><span class="o">*</span>,<span class="o">*</span><span class="ss">)</span><span class="s1">&#39;</span><span class="s">th =&gt; TH    : </span><span class="s1">&#39;</span><span class="o">//</span><span class="nv">trim</span><span class="ss">(</span><span class="nv">targetline</span><span class="ss">)</span>

<span class="o">!</span> <span class="nv">a</span> <span class="nv">null</span> <span class="nv">old</span> <span class="nv">substring</span> <span class="nv">means</span> <span class="s2">&quot;</span><span class="s">at beginning of line</span><span class="s2">&quot;</span>
<span class="o">!</span> <span class="nv">changes</span> <span class="nv">the</span> <span class="nv">input</span> <span class="nv">to</span> <span class="s1">&#39;</span><span class="s">BEFORE:this is the input string</span><span class="s1">&#39;</span>
<span class="k">call</span> <span class="nl">substitute</span><span class="ss">(</span><span class="nv">targetline</span>,<span class="s1">&#39;&#39;</span>,<span class="s1">&#39;</span><span class="s">BEFORE:</span><span class="s1">&#39;</span><span class="ss">)</span>
<span class="nv">write</span><span class="ss">(</span><span class="o">*</span>,<span class="o">*</span><span class="ss">)</span><span class="s1">&#39;</span><span class="s">&quot;&quot; =&gt; BEFORE: </span><span class="s1">&#39;</span><span class="o">//</span><span class="nv">trim</span><span class="ss">(</span><span class="nv">targetline</span><span class="ss">)</span>

<span class="o">!</span> <span class="nv">a</span> <span class="nv">null</span> <span class="nv">new</span> <span class="nv">string</span> <span class="nv">deletes</span> <span class="nv">occurrences</span> <span class="nv">of</span> <span class="nv">the</span> <span class="nv">old</span> <span class="nv">substring</span>
<span class="o">!</span> <span class="nv">changes</span> <span class="nv">the</span> <span class="nv">input</span> <span class="nv">to</span> <span class="s1">&#39;</span><span class="s">ths s the nput strng</span><span class="s1">&#39;</span>
<span class="k">call</span> <span class="nl">substitute</span><span class="ss">(</span><span class="nv">targetline</span>,<span class="s1">&#39;</span><span class="s">i</span><span class="s1">&#39;</span>,<span class="s1">&#39;&#39;</span><span class="ss">)</span>
<span class="nv">write</span><span class="ss">(</span><span class="o">*</span>,<span class="o">*</span><span class="ss">)</span><span class="s1">&#39;</span><span class="s">i =&gt; &quot;&quot;     : </span><span class="s1">&#39;</span><span class="o">//</span><span class="nv">trim</span><span class="ss">(</span><span class="nv">targetline</span><span class="ss">)</span>

<span class="k">end</span> <span class="nv">program</span> <span class="nv">demo_substitute</span>
</code></pre></div>

<p>Expected output</p>
<div class="codehilite"><pre><span></span><code> ORIGINAL    : this is the input string
 th =&gt; TH    : THis is THe input string
 &quot;&quot; =&gt; BEFORE: BEFORE:THis is THe input string
 i =&gt; &quot;&quot;     : BEFORE:THs s THe nput strng
</code></pre></div>

<h2 id="author">AUTHOR</h2>
<div class="codehilite"><pre><span></span><code>John S. Urban
</code></pre></div>

<h2 id="license">LICENSE</h2>
<div class="codehilite"><pre><span></span><code>Public Domain
</code></pre></div>
    

    <h3>Arguments</h3>
    
      
<table class="table table-striped varlist">
<thead><tr><th>Type</th>
<th>Intent</th><th>Optional</th>
<th>Attributes</th><th></th><th>Name</th><th></th></thead>



<tbody>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-targetline~2"></span>character(len=*)</td>
  <td></td>
  <td></td>
  
  <td></td><td>::</td>
  <td><strong>targetline</strong></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-old~3"></span>character(len=*),</td>
  <td>intent(in)</td>
  <td></td>
  
  <td></td><td>::</td>
  <td><strong>old</strong></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-new~3"></span>character(len=*),</td>
  <td>intent(in)</td>
  <td></td>
  
  <td></td><td>::</td>
  <td><strong>new</strong></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-ierr~8"></span>integer,</td>
  <td>intent(out),</td>
  <td>optional</td>
  
  <td></td><td>::</td>
  <td><strong>ierr</strong></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-start~2"></span>integer,</td>
  <td>intent(in),</td>
  <td>optional</td>
  
  <td></td><td>::</td>
  <td><strong>start</strong></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-end~4"></span>integer,</td>
  <td>intent(in),</td>
  <td>optional</td>
  
  <td></td><td>::</td>
  <td><strong>end</strong></td><td></td>
  
</tr>

</tbody>
</table>

    
    
    
    
     
    <br>

    <section class="visible-xs visible-sm hidden-md">
      
<h3>Contents</h3>
 



<div class="panel panel-primary">
  <div class="panel-heading text-left"><h3 class="panel-title"><a data-toggle="collapse" href="#vars-1">Variables</a></h3></div>
  <div id="vars-1" class="panel-collapse collapse">
    <div class="list-group">
      
      <a class="list-group-item" href="../proc/substitute.html#variable-dum1">dum1</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ic~4">ic</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ichr~3">ichr</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-id~2">id</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ier1">ier1</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-il">il</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ind~2">ind</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ir">ir</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ladd~2">ladd</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-len_new~2">len_new</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-len_old~2">len_old</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-maxlengthout">maxlengthout</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-ml">ml</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-mr">mr</a>
      
      <a class="list-group-item" href="../proc/substitute.html#variable-original_input_length~2">original_input_length</a>
      
    </div>
  </div>
</div>




















<div class="panel panel-primary">
  <div class="panel-heading text-left"><h3 class="panel-title">Source Code</h3></div>
  <div class="list-group">
    <a class="list-group-item" href="../proc/substitute.html#src">substitute</a>
  </div>
</div>



    </section>
    <br class="visible-xs visible-sm hidden-md">

    

    
    <section>    
      <h2>Variables</h2>
    
<table class="table table-striped varlist">
<thead><tr><th>Type</th><th>Visibility</th>

<th>Attributes</th><th></th><th>Name</th><th></th><th>Initial</th><th></th></thead>



<tbody>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-dum1"></span>character(len=len(targetline)),</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>dum1</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-ic~4"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>ic</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-ichr~3"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>ichr</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-id~2"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>id</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-ier1"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>ier1</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-il"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>il</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-ind~2"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>ind</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-ir"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>ir</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-ladd~2"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>ladd</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-len_new~2"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>len_new</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-len_old~2"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>len_old</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-maxlengthout"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>maxlengthout</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-ml"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>ml</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-mr"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>mr</strong></td><td></td><td></td><td></td>
  
</tr>

  
  
  
  
<tr>
  
  <td><span class="anchor" id="variable-original_input_length~2"></span>integer,</td>
  
  <td>public</td>
  <td></td><td>::</td>
  <td><strong>original_input_length</strong></td><td></td><td></td><td></td>
  
</tr>

</tbody>
</table>

    </section>
    <br>
    
    
    
    
    

    
    
    
    
    


    
    
    
    <section>
    <h2><span class="anchor" id="src"></span>Source Code</h2>
    <div class="highlight"><pre><span></span><span class="k">subroutine </span><span class="n">substitute</span><span class="p">(</span><span class="n">targetline</span><span class="p">,</span><span class="n">old</span><span class="p">,</span><span class="n">new</span><span class="p">,</span><span class="n">ierr</span><span class="p">,</span><span class="n">start</span><span class="p">,</span><span class="k">end</span><span class="p">)</span>

<span class="c">! ident_12=&quot;@(#)M_strings::substitute(3f): Globally substitute one substring for another in string&quot;</span>

<span class="c">!-----------------------------------------------------------------------------------------------------------------------------------</span>
<span class="kt">character</span><span class="p">(</span><span class="nb">len</span><span class="o">=*</span><span class="p">)</span>               <span class="kd">::</span> <span class="n">targetline</span>         <span class="c">! input line to be changed</span>
<span class="kt">character</span><span class="p">(</span><span class="nb">len</span><span class="o">=*</span><span class="p">),</span><span class="k">intent</span><span class="p">(</span><span class="n">in</span><span class="p">)</span>    <span class="kd">::</span> <span class="n">old</span>                <span class="c">! old substring to replace</span>
<span class="kt">character</span><span class="p">(</span><span class="nb">len</span><span class="o">=*</span><span class="p">),</span><span class="k">intent</span><span class="p">(</span><span class="n">in</span><span class="p">)</span>    <span class="kd">::</span> <span class="n">new</span>                <span class="c">! new substring</span>
<span class="kt">integer</span><span class="p">,</span><span class="k">intent</span><span class="p">(</span><span class="n">out</span><span class="p">),</span><span class="k">optional</span>   <span class="kd">::</span> <span class="n">ierr</span>               <span class="c">! error code. if ierr = -1 bad directive, &gt;=0 then ierr changes made</span>
<span class="kt">integer</span><span class="p">,</span><span class="k">intent</span><span class="p">(</span><span class="n">in</span><span class="p">),</span><span class="k">optional</span>    <span class="kd">::</span> <span class="n">start</span>              <span class="c">! start sets the left margin</span>
<span class="kt">integer</span><span class="p">,</span><span class="k">intent</span><span class="p">(</span><span class="n">in</span><span class="p">),</span><span class="k">optional</span>    <span class="kd">::</span> <span class="k">end</span>                <span class="c">! end sets the right margin</span>
<span class="c">!-----------------------------------------------------------------------------------------------------------------------------------</span>
<span class="kt">character</span><span class="p">(</span><span class="nb">len</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">targetline</span><span class="p">))</span> <span class="kd">::</span> <span class="n">dum1</span>               <span class="c">! scratch string buffers</span>
<span class="kt">integer</span>                        <span class="kd">::</span> <span class="n">ml</span><span class="p">,</span> <span class="n">mr</span><span class="p">,</span> <span class="n">ier1</span>
<span class="kt">integer</span>                        <span class="kd">::</span> <span class="n">maxlengthout</span>       <span class="c">! MAXIMUM LENGTH ALLOWED FOR NEW STRING</span>
<span class="kt">integer</span>                        <span class="kd">::</span> <span class="n">original_input_length</span>
<span class="kt">integer</span>                        <span class="kd">::</span> <span class="n">len_old</span><span class="p">,</span> <span class="n">len_new</span>
<span class="kt">integer</span>                        <span class="kd">::</span> <span class="n">ladd</span>
<span class="kt">integer</span>                        <span class="kd">::</span> <span class="n">ir</span>
<span class="kt">integer</span>                        <span class="kd">::</span> <span class="n">ind</span>
<span class="kt">integer</span>                        <span class="kd">::</span> <span class="n">il</span>
<span class="kt">integer</span>                        <span class="kd">::</span> <span class="n">id</span>
<span class="kt">integer</span>                        <span class="kd">::</span> <span class="n">ic</span>
<span class="kt">integer</span>                        <span class="kd">::</span> <span class="n">ichr</span>
<span class="c">!-----------------------------------------------------------------------------------------------------------------------------------</span>
   <span class="k">if</span> <span class="p">(</span><span class="nb">present</span><span class="p">(</span><span class="n">start</span><span class="p">))</span> <span class="k">then</span>                            <span class="c">! optional starting column</span>
      <span class="n">ml</span><span class="o">=</span><span class="n">start</span>
   <span class="k">else</span>
<span class="k">      </span><span class="n">ml</span><span class="o">=</span><span class="mi">1</span>
   <span class="k">endif</span>
<span class="k">   if</span> <span class="p">(</span><span class="nb">present</span><span class="p">(</span><span class="k">end</span><span class="p">))</span> <span class="k">then</span>                              <span class="c">! optional ending column</span>
      <span class="n">mr</span><span class="o">=</span><span class="k">end</span>
<span class="k">   else</span>
<span class="k">      </span><span class="n">mr</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">targetline</span><span class="p">)</span>
   <span class="k">endif</span>
<span class="c">!-----------------------------------------------------------------------------------------------------------------------------------</span>
   <span class="n">ier1</span><span class="o">=</span><span class="mi">0</span>                                              <span class="c">! initialize error flag/change count</span>
   <span class="n">maxlengthout</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">targetline</span><span class="p">)</span>                        <span class="c">! max length of output string</span>
   <span class="n">original_input_length</span><span class="o">=</span><span class="nb">len_trim</span><span class="p">(</span><span class="n">targetline</span><span class="p">)</span>          <span class="c">! get non-blank length of input line</span>
   <span class="n">dum1</span><span class="p">(:)</span><span class="o">=</span><span class="s1">&#39; &#39;</span>                                         <span class="c">! initialize string to build output in</span>
   <span class="n">id</span><span class="o">=</span><span class="n">mr</span><span class="o">-</span><span class="n">ml</span>                                            <span class="c">! check for window option ! change to optional parameter(s)</span>
<span class="c">!-----------------------------------------------------------------------------------------------------------------------------------</span>
   <span class="n">len_old</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">old</span><span class="p">)</span>                                    <span class="c">! length of old substring to be replaced</span>
   <span class="n">len_new</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">new</span><span class="p">)</span>                                    <span class="c">! length of new substring to replace old substring</span>
   <span class="k">if</span><span class="p">(</span><span class="n">id</span><span class="p">.</span><span class="n">le</span><span class="p">.</span><span class="mi">0</span><span class="p">)</span><span class="k">then</span>                                     <span class="c">! no window so change entire input string</span>
      <span class="n">il</span><span class="o">=</span><span class="mi">1</span>                                             <span class="c">! il is left margin of window to change</span>
      <span class="n">ir</span><span class="o">=</span><span class="n">maxlengthout</span>                                  <span class="c">! ir is right margin of window to change</span>
      <span class="n">dum1</span><span class="p">(:)</span><span class="o">=</span><span class="s1">&#39; &#39;</span>                                      <span class="c">! begin with a blank line</span>
   <span class="k">else</span>                                                <span class="c">! if window is set</span>
      <span class="n">il</span><span class="o">=</span><span class="n">ml</span>                                            <span class="c">! use left margin</span>
      <span class="n">ir</span><span class="o">=</span><span class="n">min0</span><span class="p">(</span><span class="n">mr</span><span class="p">,</span><span class="n">maxlengthout</span><span class="p">)</span>                         <span class="c">! use right margin or rightmost</span>
      <span class="n">dum1</span><span class="o">=</span><span class="n">targetline</span><span class="p">(:</span><span class="n">il</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>                           <span class="c">! begin with what&#39;s below margin</span>
   <span class="k">endif</span>                                               <span class="c">! end of window settings</span>
<span class="c">!-----------------------------------------------------------------------------------------------------------------------------------</span>
   <span class="k">if</span><span class="p">(</span><span class="n">len_old</span><span class="p">.</span><span class="n">eq</span><span class="p">.</span><span class="mi">0</span><span class="p">)</span><span class="k">then</span>                                <span class="c">! c//new/ means insert new at beginning of line (or left margin)</span>
      <span class="n">ichr</span><span class="o">=</span><span class="n">len_new</span> <span class="o">+</span> <span class="n">original_input_length</span>
      <span class="k">if</span><span class="p">(</span><span class="n">ichr</span><span class="p">.</span><span class="n">gt</span><span class="p">.</span><span class="n">maxlengthout</span><span class="p">)</span><span class="k">then</span>
<span class="k">         call </span><span class="n">journal</span><span class="p">(</span><span class="s1">&#39;sc&#39;</span><span class="p">,</span><span class="s1">&#39;*substitute* new line will be too long&#39;</span><span class="p">)</span>
         <span class="n">ier1</span><span class="o">=-</span><span class="mi">1</span>
         <span class="k">if</span> <span class="p">(</span><span class="nb">present</span><span class="p">(</span><span class="n">ierr</span><span class="p">))</span><span class="n">ierr</span><span class="o">=</span><span class="n">ier1</span>
         <span class="k">return</span>
<span class="k">      endif</span>
<span class="k">      if</span><span class="p">(</span><span class="n">len_new</span><span class="p">.</span><span class="n">gt</span><span class="p">.</span><span class="mi">0</span><span class="p">)</span><span class="k">then</span>
<span class="k">         </span><span class="n">dum1</span><span class="p">(</span><span class="n">il</span><span class="p">:)</span><span class="o">=</span><span class="n">new</span><span class="p">(:</span><span class="n">len_new</span><span class="p">)</span><span class="o">//</span><span class="n">targetline</span><span class="p">(</span><span class="n">il</span><span class="p">:</span><span class="n">original_input_length</span><span class="p">)</span>
      <span class="k">else</span>
<span class="k">         </span><span class="n">dum1</span><span class="p">(</span><span class="n">il</span><span class="p">:)</span><span class="o">=</span><span class="n">targetline</span><span class="p">(</span><span class="n">il</span><span class="p">:</span><span class="n">original_input_length</span><span class="p">)</span>
      <span class="k">endif</span>
<span class="k">      </span><span class="n">targetline</span><span class="p">(</span><span class="mi">1</span><span class="p">:</span><span class="n">maxlengthout</span><span class="p">)</span><span class="o">=</span><span class="n">dum1</span><span class="p">(:</span><span class="n">maxlengthout</span><span class="p">)</span>
      <span class="n">ier1</span><span class="o">=</span><span class="mi">1</span>                                           <span class="c">! made one change. actually, c/// should maybe return 0</span>
      <span class="k">if</span><span class="p">(</span><span class="nb">present</span><span class="p">(</span><span class="n">ierr</span><span class="p">))</span><span class="n">ierr</span><span class="o">=</span><span class="n">ier1</span>
      <span class="k">return</span>
<span class="k">   endif</span>
<span class="c">!-----------------------------------------------------------------------------------------------------------------------------------</span>
   <span class="n">ichr</span><span class="o">=</span><span class="n">il</span>                                            <span class="c">! place to put characters into output string</span>
   <span class="n">ic</span><span class="o">=</span><span class="n">il</span>                                               <span class="c">! place looking at in input string</span>
   <span class="n">loop</span><span class="p">:</span> <span class="k">do</span>
<span class="k">      </span><span class="n">ind</span><span class="o">=</span><span class="nb">index</span><span class="p">(</span><span class="n">targetline</span><span class="p">(</span><span class="n">ic</span><span class="p">:),</span><span class="n">old</span><span class="p">(:</span><span class="n">len_old</span><span class="p">))</span><span class="o">+</span><span class="n">ic</span><span class="o">-</span><span class="mi">1</span>    <span class="c">! try to find start of old string in remaining part of input in change window</span>
      <span class="k">if</span><span class="p">(</span><span class="n">ind</span><span class="p">.</span><span class="n">eq</span><span class="p">.</span><span class="n">ic</span><span class="o">-</span><span class="mf">1.</span><span class="nb">or</span><span class="p">.</span><span class="n">ind</span><span class="p">.</span><span class="n">gt</span><span class="p">.</span><span class="n">ir</span><span class="p">)</span><span class="k">then</span>                 <span class="c">! did not find old string or found old string past edit window</span>
         <span class="k">exit </span><span class="n">loop</span>                                     <span class="c">! no more changes left to make</span>
      <span class="k">endif</span>
<span class="k">      </span><span class="n">ier1</span><span class="o">=</span><span class="n">ier1</span><span class="o">+</span><span class="mi">1</span>                                      <span class="c">! found an old string to change, so increment count of changes</span>
      <span class="k">if</span><span class="p">(</span><span class="n">ind</span><span class="p">.</span><span class="n">gt</span><span class="p">.</span><span class="n">ic</span><span class="p">)</span><span class="k">then</span>                                <span class="c">! if found old string past at current position in input string copy unchanged</span>
         <span class="n">ladd</span><span class="o">=</span><span class="n">ind</span><span class="o">-</span><span class="n">ic</span>                                   <span class="c">! find length of character range to copy as-is from input to output</span>
         <span class="k">if</span><span class="p">(</span><span class="n">ichr</span><span class="o">-</span><span class="mi">1</span><span class="o">+</span><span class="n">ladd</span><span class="p">.</span><span class="n">gt</span><span class="p">.</span><span class="n">maxlengthout</span><span class="p">)</span><span class="k">then</span>
<span class="k">            </span><span class="n">ier1</span><span class="o">=-</span><span class="mi">1</span>
            <span class="k">exit </span><span class="n">loop</span>
         <span class="k">endif</span>
<span class="k">         </span><span class="n">dum1</span><span class="p">(</span><span class="n">ichr</span><span class="p">:)</span><span class="o">=</span><span class="n">targetline</span><span class="p">(</span><span class="n">ic</span><span class="p">:</span><span class="n">ind</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
         <span class="n">ichr</span><span class="o">=</span><span class="n">ichr</span><span class="o">+</span><span class="n">ladd</span>
      <span class="k">endif</span>
<span class="k">      if</span><span class="p">(</span><span class="n">ichr</span><span class="o">-</span><span class="mi">1</span><span class="o">+</span><span class="n">len_new</span><span class="p">.</span><span class="n">gt</span><span class="p">.</span><span class="n">maxlengthout</span><span class="p">)</span><span class="k">then</span>
<span class="k">         </span><span class="n">ier1</span><span class="o">=-</span><span class="mi">2</span>
         <span class="k">exit </span><span class="n">loop</span>
      <span class="k">endif</span>
<span class="k">      if</span><span class="p">(</span><span class="n">len_new</span><span class="p">.</span><span class="n">ne</span><span class="p">.</span><span class="mi">0</span><span class="p">)</span><span class="k">then</span>
<span class="k">         </span><span class="n">dum1</span><span class="p">(</span><span class="n">ichr</span><span class="p">:)</span><span class="o">=</span><span class="n">new</span><span class="p">(:</span><span class="n">len_new</span><span class="p">)</span>
         <span class="n">ichr</span><span class="o">=</span><span class="n">ichr</span><span class="o">+</span><span class="n">len_new</span>
      <span class="k">endif</span>
<span class="k">      </span><span class="n">ic</span><span class="o">=</span><span class="n">ind</span><span class="o">+</span><span class="n">len_old</span>
   <span class="k">enddo </span><span class="n">loop</span>
<span class="c">!-----------------------------------------------------------------------------------------------------------------------------------</span>
   <span class="k">select case</span> <span class="p">(</span><span class="n">ier1</span><span class="p">)</span>
   <span class="k">case</span> <span class="p">(:</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
      <span class="k">call </span><span class="n">journal</span><span class="p">(</span><span class="s1">&#39;sc&#39;</span><span class="p">,</span><span class="s1">&#39;*substitute* new line will be too long&#39;</span><span class="p">)</span>
   <span class="k">case</span> <span class="p">(</span><span class="mi">0</span><span class="p">)</span>                                                <span class="c">! there were no changes made to the window</span>
   <span class="k">case </span><span class="n">default</span>
      <span class="n">ladd</span><span class="o">=</span><span class="n">original_input_length</span><span class="o">-</span><span class="n">ic</span>
      <span class="k">if</span><span class="p">(</span><span class="n">ichr</span><span class="o">+</span><span class="n">ladd</span><span class="p">.</span><span class="n">gt</span><span class="p">.</span><span class="n">maxlengthout</span><span class="p">)</span><span class="k">then</span>
<span class="k">         call </span><span class="n">journal</span><span class="p">(</span><span class="s1">&#39;sc&#39;</span><span class="p">,</span><span class="s1">&#39;*substitute* new line will be too long&#39;</span><span class="p">)</span>
         <span class="n">ier1</span><span class="o">=-</span><span class="mi">1</span>
         <span class="k">if</span><span class="p">(</span><span class="nb">present</span><span class="p">(</span><span class="n">ierr</span><span class="p">))</span><span class="n">ierr</span><span class="o">=</span><span class="n">ier1</span>
         <span class="k">return</span>
<span class="k">      endif</span>
<span class="k">      if</span><span class="p">(</span><span class="n">ic</span><span class="p">.</span><span class="n">lt</span><span class="p">.</span><span class="nb">len</span><span class="p">(</span><span class="n">targetline</span><span class="p">))</span><span class="k">then</span>
<span class="k">         </span><span class="n">dum1</span><span class="p">(</span><span class="n">ichr</span><span class="p">:)</span><span class="o">=</span><span class="n">targetline</span><span class="p">(</span><span class="n">ic</span><span class="p">:</span><span class="nb">max</span><span class="p">(</span><span class="n">ic</span><span class="p">,</span><span class="n">original_input_length</span><span class="p">))</span>
      <span class="k">endif</span>
<span class="k">      </span><span class="n">targetline</span><span class="o">=</span><span class="n">dum1</span><span class="p">(:</span><span class="n">maxlengthout</span><span class="p">)</span>
   <span class="k">end select</span>
<span class="k">   if</span><span class="p">(</span><span class="nb">present</span><span class="p">(</span><span class="n">ierr</span><span class="p">))</span><span class="n">ierr</span><span class="o">=</span><span class="n">ier1</span>
<span class="c">!-----------------------------------------------------------------------------------------------------------------------------------</span>
<span class="k">end subroutine </span><span class="n">substitute</span>
</pre></div>

    </section>
    <br>
    
    
    </div>
  </div>


    <hr>    
    </div> <!-- /container -->
    <footer>
      <div class="container">
      <div class="row">
        <div class="col-xs-6 col-md-4"><p>&copy; 2022 
                                          </p></div>
        <div class="col-xs-6 col-md-4 col-md-push-4">
          <p class="text-right">
            Documentation generated by 
            <a href="https://github.com/cmacmackin/ford">FORD</a>
             on 2022-01-16 22:32  
          </p>
        </div>
        <div class="col-xs-12 col-md-4 col-md-pull-4"><p class="text-center"> M_strings was developed by John S. Urban</p></div>
      </div>
      <br>
      </div> <!-- /container -->    
    </footer>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
<!--
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
-->
    <script src="../js/bootstrap.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="../js/ie10-viewport-bug-workaround.js"></script>

    <!-- MathJax JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        TeX: { extensions: ['AMSmath.js','AMSsymbols.js','noErrors.js','noUndefined.js'], equationNumbers: { autoNumber: 'AMS' } },
        jax: ['input/TeX','input/MathML','output/HTML-CSS'],
        extensions: ['tex2jax.js','mml2jax.js','MathMenu.js','MathZoom.js']
      });
    </script>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    
    <script src="../tipuesearch/tipuesearch_content.js"></script>
    <script src="../tipuesearch/tipuesearch_set.js"></script>
    <script src="../tipuesearch/tipuesearch.js"></script>
    
    
  </body>
</html>